package android.androidapp;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class MyLocationListener implements LocationListener {

	private MapController mc;
	private MapView mapView;
	private Context currentContext;
	private Startup startActivity;
	
	public MyLocationListener(MapController mc, MapView mapView, Startup start)
	{
		this.mc = mc;
		this.mapView = mapView;
		this.currentContext = mapView.getContext();
		this.startActivity = start;
	}

	@Override
    public void onLocationChanged(Location loc) 
    {
      if (loc != null) 
      { 
    	  double currentLat = loc.getLatitude();
    	  double currentLong = loc.getLongitude();
    	  
        Toast.makeText(currentContext, 
          "Location changed : Lat: " + currentLat + 
          " Lng: " + currentLong, 
          Toast.LENGTH_SHORT).show();
        
	    double longitudeUcfMax = -81.20754718780518 * 1E6;
	    double longitudeUcfMin = -81.18787050247192 * 1E6;
	    double latitudeUcfMax = 28.607751640540673 * 1E6;
	    double latitudeUcfMin = 28.590795999742777 * 1E6;
	    
	    try
	    {
		    //if((int)(currentLat * 1E6) > (int)latitudeUcfMax || 
		    //		(int)(currentLat * 1E6) < (int)latitudeUcfMin ||
		    //		(int)(currentLong * 1E6) < (int)longitudeUcfMin ||
		    //		(int)(currentLong * 1E6) > (int)longitudeUcfMax)
		    //{
		    	//show out of range view
		    //	return;
		    //}
		    //else
		    //{
		    	//Update camera view
		        GeoPoint p = new GeoPoint(
		        (int) (currentLat * 1E6), 
		        (int) (currentLong * 1E6));
		        mc.animateTo(p);   
			    mapView.invalidate();
		        
		        //Update current position on map with point
		        if(this.startActivity.itemizedOverlayCurrent ==null)
		        {
				    Drawable drawableNear = this.startActivity.getResources().getDrawable(R.drawable.map_pin_target);
				    
				    this.startActivity.itemizedOverlayCurrent = new CustomItemizedOverlay(drawableNear,mapView);
		        }
			    
		        this.startActivity.itemizedOverlayCurrent.clearOverlay();
			    OverlayItem overlayitemCurrent = new OverlayItem(p, "ME!", "ME!");
			    this.startActivity.itemizedOverlayCurrent.addOverlay(overlayitemCurrent);
		        
		        //Update all points shown.
		        this.startActivity.updateOverlayWithClosestPoints(currentLat* 1E6,currentLong* 1E6);
		        
			    
		    //}
	   }
	   catch(Exception e)
	   {
		   Log.e("ERROR", e.toString());
	   }
	   
      }
    }

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

}
